package com.base.utils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public abstract class OutputExcel {
	
	public abstract void writeWorkbook(HSSFWorkbook workbook);
	
	private Object data;
	private Object obj;
	
	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public OutputExcel(){
		
	}
	
	public OutputExcel(Object data){
		this.data=data;
	}
	
	public OutputExcel(Object data,Object obj){
		this.data=data;
		this.obj=obj;
	}
	
	public void writeToResponse(String filename,HttpServletRequest request,HttpServletResponse response){
		HSSFWorkbook workbook=new HSSFWorkbook();
		writeWorkbook(workbook);
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="+transformString(filename,request)+".xls");
		try {
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 创建表头样式
	 * @param HSSFWorkbook
	 * @return HSSFCellStyle
	 */
	public  HSSFCellStyle createHeadCellStyle(HSSFWorkbook  workbook){
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints((short) 14);
		font.setFontName("宋体");
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		cellStyle.setWrapText(true);
		return cellStyle;
	}
	/**
	 * 创建 表格行样式
	 * @param HSSFWorkbook
	 * @return workbook
	 */
	public  HSSFCellStyle createRowCellStyle(HSSFWorkbook  workbook){
		HSSFFont font1 = workbook.createFont();
		font1.setFontHeightInPoints((short) 12);
		font1.setFontName("宋体");

		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font1);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		cellStyle.setWrapText(true);
		return cellStyle;
	}
	/**
	 * 创建单元格并赋值
	 * @param row
	 * @param value
	 * @param style
	 */
	@SuppressWarnings("deprecation")
	public void createCell(HSSFRow row,short col,String value,HSSFCellStyle style){
		HSSFCell cell = row.createCell(col);
		cell.setCellValue(new HSSFRichTextString(value));
		cell.setCellStyle(style);
	}
	/**
	 * 创建单元格并赋值
	 * @param row
	 * @param value
	 * @param style
	 */
	@SuppressWarnings("deprecation")
	public void createCells(HSSFRow row,short start,String[] value,HSSFCellStyle style){
		for(int i=0;i<value.length;i++){
			HSSFCell cell = row.createCell(start++);
			cell.setCellValue(new HSSFRichTextString(value[i]));
			cell.setCellStyle(style);
		}
	}
	
	/**
	 * 不同浏览器导出名称转码
	 * @param filename
	 * @param request
	 * @return
	 */
	public static String transformString(String filename,HttpServletRequest request){
		String agent=request.getHeader("USER-AGENT");
		String tempname;
		try {
			if (null != agent && -1 != agent.indexOf("MSIE")) {
				// ie浏览器
				return java.net.URLEncoder.encode(filename, "UTF8");
	
			} else if (null != agent && -1 != agent.indexOf("Mozilla")) {
				// firefox
				tempname = new String(filename.getBytes(), "ISO8859-1");
				java.net.URLEncoder.encode(tempname, "ISO8859-1"); // 这句很重要，不然文件名为乱码
				return tempname;
			} else {
				return filename;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return filename;
	}
}
